-
Notifications
You must be signed in to change notification settings - Fork 20k
Add RandomizedMatrixVerifier and test class #6305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add RandomizedMatrixVerifier and test class #6305
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #6305 +/- ##
============================================
+ Coverage 74.16% 74.18% +0.02%
- Complexity 5362 5372 +10
============================================
Files 678 679 +1
Lines 18686 18709 +23
Branches 3625 3634 +9
============================================
+ Hits 13858 13880 +22
- Misses 4273 4274 +1
Partials 555 555 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
int[] r = new int[n]; | ||
|
||
// Generate random vector r | ||
for (int i = 0; i < n; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@krishnamitra03 Freivalds' algorithm usually uses a random binary vector (entries 0 or 1), not values in range 0-9. Using Math.random() * 10 may lead to inefficiencies or poor randomness.
for (int i = 0; i < n; i++) { | ||
r[i] = (int) (Math.random() * 10); // keep it simple | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider checking if matrices A, B, and C are all square and of the same size.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please fix the failing build pipeline (checkstyle)
} | ||
} | ||
assertFalse(result, "Verification should return false for incorrect C"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test for mismatched sizes to check dimension preconditions (if implemented)
@TheAlgorithms/java_maintainers Would you prefer placing randomized algorithms like RandomizedMatrixVerifier or RandomizedQuickSort in their respective functional packages (e.g., matrix, sorting) to keep related algorithms together, or should we collect all randomized algorithms in a dedicated randomized package for conceptual clarity? |
I'd say store them in functional packages because then it's organized based on problem and not approach used to solve it |
New Feature: Randomized Matrix Equality Verifier
This pull request introduces a new utility class called RandomizedMatrixVerifier in the matrix package.
It provides an efficient method areMatricesEqual(int[][] a, int[][] b) that determines whether two matrices are equal using randomized hashing with prime coefficients.
This approach ensures performance and robustness, especially for large matrices.
Tests Added:-
A corresponding test class RandomizedMatrixVerifierTest is added under src/test/java/com/thealgorithms/matrix/.
It includes two unit tests using JUnit 5:
✅ Verifies equality for identical matrices
❌ Verifies inequality for different matrices
Both test cases pass successfully using the Maven test runner.
Build & Code Coverage:-
The project builds cleanly with Java 21 and Maven 3.9.10
Tests executed via mvn -Dtest=RandomizedMatrixVerifierTest test
Code coverage verified using JaCoCo, confirming the new code is tested
Code Quality & Structure:-
Follows the existing file structure and naming conventions of the repository
Code is clean, well-commented, and adheres to contribution guidelines
No breaking changes introduced to existing functionality
About This Contribution:-
This is my first contribution to the TheAlgorithms/Java repository
I’m currently learning open-source contribution and Java best practices.
Feedback and suggestions are warmly welcome!!!